home *** CD-ROM | disk | FTP | other *** search
- #ifndef __LISTENINGAE__
- #include "ListeningAE.h"
- #endif
-
- OSErr SetSpeechDoneAEHander (SpeechInfoPtr theSpeechInfo)
- {
- OSErr theErr = noErr;
-
- if (theSpeechInfo->SpeechDoneUPP != nil) {
- DisposeRoutineDescriptor (theSpeechInfo->SpeechDoneUPP);
- }
- theSpeechInfo->SpeechDoneUPP = NewAEEventHandlerProc (HandleSpeechDoneAppleEvent);
-
- return theErr;
- }
-
- OSErr InstallRecogAEHandler (SpeechInfoPtr theSpeechInfo)
- {
- OSErr theErr = noErr;
-
- /* Install an AppleEvent handler so recognizer can send us recognition results. */
- theErr = AEInstallEventHandler(kAESpeechSuite, kAESpeechDone, theSpeechInfo->SpeechDoneUPP, (long)theSpeechInfo, false);
-
- return theErr;
- }
-
- pascal OSErr HandleSpeechDoneAppleEvent (AppleEvent *theAEevt, AppleEvent* reply, SpeechInfoPtr theSpeechInfo)
- {
- #pragma unused (reply)
-
- SRRecognitionResult recResult;
- DescType actualType;
- long actualSize;
- OSErr theErr = noErr,
- recStatus = noErr;
-
- /* Get status */
- theErr = AEGetParamPtr (theAEevt, keySRSpeechStatus, typeShortInteger, &actualType, (Ptr)&recStatus, sizeof(recStatus), &actualSize);
-
- /* Get result */
- if (theErr == noErr && recStatus == noErr) {
- theErr = AEGetParamPtr (theAEevt, keySRSpeechResult, typeSRSpeechResult, &actualType, (Ptr)&recResult, sizeof(SRRecognitionResult), &actualSize);
- }
- if (theErr == noErr) {
- theErr = ParseResult (&recResult, theSpeechInfo);
- }
-
- return theErr;
- }
-
- /*
- Here we figure out what the user said and act on what they said.
- */
- OSErr ParseResult (SRRecognitionResult *recResult, SpeechInfoPtr theSpeechInfo)
- {
- SRLanguageModel theResultLM;
- SRPath theResultPath;
- SRLanguageObject theSubElement;
- SRSpeechObject speechRefCon = 0;
- long count = 0,
- i;
- Size theLen;
- OSErr theErr = noErr;
-
- theLen = sizeof (theResultLM);
- theErr = SRGetProperty (*recResult, kSRLanguageModelFormat, &theResultLM, &theLen);
- if (theErr == noErr) {
- theLen = sizeof (theResultPath);
- theErr = SRGetProperty (*recResult, kSRPathFormat, &theResultPath, &theLen);
- if (theErr == noErr) {
- theErr = SRCountItems (theResultPath, &count);
- }
- if (theErr == noErr) {
- for (i = 0; i < count; i++) {
- theLen = sizeof (speechRefCon);
- theErr = SRGetIndexedItem (theResultPath, &theSubElement, i);
- if (theErr == noErr) {
- theErr = SRGetProperty (theSubElement, kSRRefCon, &speechRefCon, &theLen);
- if (speechRefCon != 0) {
- /* ParseRefCon is the routine that does the work of doing what the
- user said to do. */
- theErr = ParseRefCon (theSpeechInfo, (CommandPtr)speechRefCon);
- SRReleaseObject (speechRefCon);
- }
- SRReleaseObject (theSubElement);
- }
- }
- }
- SRReleaseObject (theResultPath);
- }
-
- SRReleaseObject (theResultLM);
- return theErr;
- }
-